//
//  BookingHotelDateCell.swift
//  DLuxTravel
//
//  Created by 陆涛 on 2018/12/3.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit
import JTAppleCalendar


struct SelectsDate {
    
    var begin:Date?
    var end:Date?
}

class BookingHotelDateCell: BaseCell {

    @IBOutlet weak var calendarView: JTAppleCalendarView!
    @IBOutlet weak var layout: UICollectionViewFlowLayout!
    
    @IBOutlet weak var monthLabel: UILabel!
    
    var selectsDate:SelectsDate = SelectsDate()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.configCell(.container)
        setupCalendarView()
        
    }
    
    private func setupCalendarView() {
        
        calendarView.register(UINib.init(nibName: "BookingHotelDateCCell", bundle: nil), forCellWithReuseIdentifier: "BookingHotelDateCCell")
        
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        calendarView.visibleDates {
            
            [weak self] visibleDates in
            guard let `self` = self else { return }
            
            self.setupMonthLabel(visibleDates: visibleDates)
        }
    }
    
    private func setupMonthLabel(visibleDates: DateSegmentInfo) {
        
        guard let date = visibleDates.monthDates.first?.date else { return }
        
        monthLabel.text = date.string("yyyy年MM月")
    }
    
    private func setupCellBG(cell:JTAppleCell, cellState:CellState) {
        
        guard let cell = cell as? BookingHotelDateCCell else { return  }
        
        let cDate = cellState.date
        
        if let startDate = selectsDate.begin {
            
            if cDate == startDate {
                
                cell.setupBG(type: .begin)
                return
            }
            
            if let endDate = selectsDate.end {
                
                if cDate == endDate {
                    
                    cell.setupBG(type: .end)
                    
                    return
                }
                
                if cDate > startDate && cDate < endDate {
                    
                    cell.setupBG(type: .center)
                    
                    return
                }
                
            }
        }
        
        cell.setupBG(type: .none)
    }
}

extension BookingHotelDateCell: JTAppleCalendarViewDataSource, JTAppleCalendarViewDelegate {
    
    
    func calendar(_ calendar: JTAppleCalendarView, willDisplay cell: JTAppleCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
        
        
    }
    
    
    func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
        
        let startDate = Date()
        
        let endDate = Date(timeIntervalSince1970: startDate.timeIntervalSince1970 + 3600 * 24 * 365)
        
        let params = ConfigurationParameters(startDate: startDate, endDate: endDate)
        return params
    }
    
    func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
        
        let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "BookingHotelDateCCell", for: indexPath) as! BookingHotelDateCCell
        
        cell.cellState = cellState
        setupCellBG(cell: cell, cellState: cellState)
        
        return cell
    }
    
    func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleCell?, cellState: CellState) {
        
        let cDate = cellState.date
        
        if let startDate = selectsDate.begin {
            
            if cDate == startDate {
                
                return
            }
            
            if nil != selectsDate.end {
                
                selectsDate.begin = cDate
                selectsDate.end = nil
            } else if cDate > startDate {
                
                selectsDate.end = cDate
            } else {
                
                selectsDate.begin = cDate
            }
            
        } else {
            
            selectsDate.begin = cDate
        }
        
        calendarView.reloadData()
    }
    
    func calendar(_ calendar: JTAppleCalendarView, didScrollToDateSegmentWith visibleDates: DateSegmentInfo) {
        
        setupMonthLabel(visibleDates: visibleDates)
    }
    
    func calendar(_ calendar: JTAppleCalendarView, willScrollToDateSegmentWith visibleDates: DateSegmentInfo) {
        
        setupMonthLabel(visibleDates: visibleDates)
    }
    
    
    
}
